/**   
* @Title: CustomerController.java 
* @Package com.csis.system.controller 
* @Description: TODO
* @author dailey  
* @date 2012-10-8
* @version V1.0   
*/
package com.csis.system.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csis.framework.util.jqgrid.JqGridModel;
import com.csis.framework.util.jqgrid.JqGridUtils;
import com.csis.system.common.WebUtil;
import com.csis.system.common.convert.Converter.ConvertArray;
import com.csis.system.domain.Customer;
import com.csis.system.domain.CustomerFormBean;
import com.csis.system.service.CustomerService;

/**
 * @author dailey
 *
 */
@Service
@RequestMapping("/system/customer")
public class CustomerController {

	protected Log log = LogFactory.getLog(getClass());
	@Resource
	private CustomerService customerService;

	final String BASE_URL = "/system/manager/admin/customer/";

	@RequestMapping("/toView")
	public String view(Model model, CustomerFormBean formBean) {
		Integer id = formBean.getBean().getId();
		Customer bean = customerService.load(id);
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "detailcust";
	}

	@RequestMapping("/toAdd")
	public String add(Model model, HttpSession session) {
		CustomerFormBean formBean = new CustomerFormBean();
		Customer bean = new Customer();
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "add";
	}

	@RequestMapping("/add")
	public @ResponseBody
	String add(CustomerFormBean formBean) {
		String result = "0";
		Customer bean = formBean.getBean();
		if (bean.getCustomerName() == null || "".equals(bean.getCustomerName().trim())) {
			return result;
		}
		try {
			customerService.save(bean);
			result = "1";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@RequestMapping("/toEdit")
	public String edit(Model model, CustomerFormBean formBean) {
		Integer id = formBean.getBean().getId();
		Customer bean = customerService.load(id);
		formBean.setBean(bean);
		model.addAttribute("formBean", formBean);
		return BASE_URL + "modify";
	}

	@RequestMapping("/edit")
	public @ResponseBody
	String edit(CustomerFormBean formBean) {
		return add(formBean);
	}

	@RequestMapping("/list")
	public String list() {
		return BASE_URL + "listcust";
	}

	@RequestMapping("/listBy")
	public void listBy(JqGridModel model, HttpServletResponse response) {
		CustomerFormBean formBean = WebUtil.buildConvert(model).convertToSingle(CustomerFormBean.class);
		response.setContentType("text/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter os = null;
		try {
			customerService.list(formBean);
			os = response.getWriter();
			os.write(JqGridUtils.getJSONParser(CustomerFormBean.class).parse(formBean).getContent());
			os.flush();
		} catch (IOException e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} catch (Exception e) {
			if (log.isErrorEnabled())
				log.error(e);
			else
				e.printStackTrace();
		} finally {
			if (os != null)
				os.close();
		}
	}

	@RequestMapping("/toDel")
	public @ResponseBody
	String remove(CustomerFormBean formBean) {
		String beanIds = formBean.getBeanIds();
		ConvertArray<Integer> ids = WebUtil.buildConvert(beanIds.split(",")).convertToArray(int.class);
		try {
			customerService.remove(ids);
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
		return "1";
	}
}
